package exogwt.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import exogwt.client.ExogwtService;
import exogwt.client.exceptions.ServiceException;
import exogwt.server.util.Exo;
import exogwt.client.domain.JcrObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.jcr.Session;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import org.exoplatform.container.SessionContainer;
import org.exoplatform.container.client.ClientInfo;
import org.exoplatform.services.jcr.RepositoryService;
import org.exoplatform.services.security.jaas.BasicCallbackHandler;

import java.io.IOException;

/**
 *
 * @author Andriy Zhdanov
 */
public class ExogwtServiceImpl extends RemoteServiceServlet implements ExogwtService {
    private RepositoryService repositoryService;
    private static final String JCR_SESSION = "exogwt.jcr.session";

    public void init() throws ServletException {
        super.init();
        repositoryService = (RepositoryService)
                Exo.getComponent(RepositoryService.class);
    }

    /**
     * @return Session.
     */
    protected final Session getSession() {
        try {
            Session session = (Session) getAttribute(JCR_SESSION);
            if (session == null || !session.isLive()) {
                Repository jcrRepository = repositoryService.getRepository();
                session = jcrRepository.login();
                setAttribute(JCR_SESSION, session);
            }
            return session;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    // Implementation of sample interface method
    public String getMessage(String msg) {
        return "Client said: \"" + msg + "\"<br>Server answered: \""
        + getSession() + "\"";
    }

    public boolean login(String login, String password) {
        try {
            LoginContext lc = new LoginContext("exo-dao",
                    new BasicCallbackHandler(login, password.toCharArray()));
            lc.login();
        } catch(LoginException e) {
            // TODO: log exception
            return false;
        }
        // TODO: use rememberMe service
        ClientInfo clientInfo = SessionContainer.getInstance().getClientInfo();
        SessionContainer.getInstance().setClientInfo(
                new ServiceClientInfo(login, clientInfo.getIpAddress()));
        return true;
    }

    public void logout() {
        ClientInfo clientInfo = SessionContainer.getInstance().getClientInfo();
        SessionContainer.getInstance().setClientInfo(
                new ServiceClientInfo(null, clientInfo.getIpAddress()));
    }


    protected final void setAttribute(String name, Object value) {
        SessionContainer.getInstance().put(name, value);
    }

    protected final Object getAttribute(String name) {
        return SessionContainer.getInstance().get(name);
    }
    
    public final void doGet(HttpServletRequest request,
        HttpServletResponse response) throws IOException {
        response.getWriter().println("login = " + login("exo", "exo"));
        response.getWriter().println("session = " + getSession());
    }
}